<template>
  <div class="price">
    <input v-model="StartValue" type="number" :placeholder="$t('common.base.price')"  @change="Start" />
    <span>~</span>
    <input v-model="LastValue" type="number" :placeholder="$t('common.base.price')"  @change="Last" />
  </div>
</template>

<script>
// API
export default {
  name: "price-range",

  data() {
    return {
      data: "",
      StartValue: "",
      LastValue: "",
    };
  },

  props: {
    value: {
      type: Array,
      default: () => [],
    },
    field: {
      type: Object,
      default: () => {},
    },
  },
  watch: {
    value: {
      deep: true,
      handler(newValue) {
        if (newValue) {
          this.StartValue = newValue[0];
          this.LastValue = newValue[1];
        }
      },
    },
  },
  mounted() {},
  methods: {
    Start() {
      if (this.StartValue === "") {
        this.SaveChange();
        return;
      }
      this.StartValue = Math.floor(Number(this.StartValue) * 100) / 100;
      if ([null, undefined, ""].includes(this.LastValue)) {
        this.SaveChange();
        return;
      }
      // 金额不能小于0
      if (this.StartValue < 0) {
        this.StartValue = 0;
      }
      if (this.StartValue > 9999999.99) {
        this.StartValue = 9999999.99;
      }

      if (this.StartValue > Number(this.LastValue)) {
        this.StartValue = this.LastValue;
      }
      this.SaveChange();
    },
    Last() {
      if (this.LastValue === "") {
        this.SaveChange();
        return;
      }
      this.LastValue = Math.floor(Number(this.LastValue) * 100) / 100;
      // 金额不能小于0
      if (this.LastValue < 0) {
        this.LastValue = 0;
      }
      if (this.LastValue > 9999999.99) {
        this.LastValue = 9999999.99;
      }
      if (this.LastValue < this.StartValue) {
        this.LastValue = this.StartValue;
      }
      this.SaveChange();
    },
    SaveChange() {
      this.$emit("update", {
        newValue: [
          this.StartValue === "" ? "" : Number(this.StartValue),
          this.LastValue === "" ? "" : Number(this.LastValue),
        ],
        oldValue: null,
        field: this.field,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-input-number.is-without-controls .el-input__inner{
  border: none;
  height: 30px;
}
.price {
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  background-color: #ffffff;
  background-image: none;
  border: 1px solid #e0e1e2;
  box-sizing: border-box;
  color: #606266;
  line-height: 40px;
  outline: none;
  border-radius: 4px;
  width: 100%;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  span {
    width: 24px;
    text-align: center;
  }
}
.price:hover {
  border-color: $color-primary-light-2;
}
.inline-input {
  width: 100%;
}
input {
  border: none;
  padding: 0;
  flex: 0 1 auto;
  width: 120px;
  text-align: center;
}
</style>
